package com.sale.system.dubbo;

import com.sale.common.core.domain.R;
import com.sale.common.core.utils.StringUtils;
import com.sale.system.api.RemoteMaterialService;
import com.sale.system.api.domain.SysApiMaterialCoding;
import com.sale.system.api.domain.SysMaterial;
import com.sale.system.api.domain.dto.FinishAndRawDto;
import com.sale.system.domain.SysMaterialCoding;
import com.sale.system.domain.dto.MaterialFinishedDto;
import com.sale.system.domain.dto.MaterialRawDto;
import com.sale.system.domain.dto.SysTheDropDto;
import com.sale.system.mapper.MaterialAuxiliaryMapper;
import com.sale.system.mapper.MaterialFinishedMapper;
import com.sale.system.mapper.MaterialRawMapper;
import com.sale.system.mapper.SysAuxiliaryUnitMapper;
import com.sale.system.service.IMaterialFinishedService;
import com.sale.system.service.IMaterialRawService;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 物料信息
 */
@RequiredArgsConstructor
@DubboService
@Service
public class RemoteMaterialServiceImpl implements RemoteMaterialService {

    private final MaterialFinishedMapper materialFinishedMapper;

    private final MaterialRawMapper materialRawMapper;

    private final IMaterialRawService materialRawService;

    private final MaterialAuxiliaryMapper materialAuxiliaryMapper;

    private final SysAuxiliaryUnitMapper sysAuxiliaryUnitMapper;

    private final IMaterialFinishedService materialFinishedService;


    /**
     * 查询物料信息
     *
     * @param
     * @return
     */
    @Override
    public SysMaterial selectMaterial(Long materialId) {
        SysMaterial sys = new SysMaterial();
        int os = materialFinishedMapper.selectCountMaterial(materialId);
        List<SysTheDropDto> listThe= sysAuxiliaryUnitMapper.selectMultiMeteringUnitAllList();
        Map<String,String> map = listThe.stream()
            .collect(Collectors.toMap(SysTheDropDto::getValue,SysTheDropDto::getLabel));
        if(os>0){
            //代表该id属于成品 查询成品信息
            MaterialFinishedDto finished = materialFinishedMapper.selectFinishedById(materialId);
            BeanUtils.copyProperties(finished, sys);
        }else{
            //代表该id属于原料 查询原料信息
            MaterialRawDto dto = materialRawMapper.selectRawById(materialId);
            BeanUtils.copyProperties(dto, sys);
        }
        String unit = sys.getMaterialUnit();
        if(StringUtils.isEmpty(unit)){
            String unit2 = sys.getMaterialMoreUnit();
            sys.setMaterialUnitDict(map.get(unit2));
        }else{
            sys.setMaterialUnitDict(map.get(unit));
        }
        sys.setAuxiliaryAttribute(materialAuxiliaryMapper.selectAuxiliaryById(materialId));
        return sys;
    }

    /**
     * 根据编码获取物料信息（财务成品调整模块使用）
     * @param materialCode
     * @return
     */
    @Override
    public FinishAndRawDto getFinishAndRaw(String materialCode) {
        return materialFinishedService.getFinishAndRaw(materialCode);
    }

    /**
     * 根据原料类别查询类别名称
     * @param codingId
     * @return
     */
    @Override
    public SysApiMaterialCoding selectCodingRulesById(String codingId) {
        SysMaterialCoding sysMaterialCoding = materialRawService.selectCodingRulesById(codingId);
        SysApiMaterialCoding sysApiMaterialCoding = new SysApiMaterialCoding();
        BeanUtils.copyProperties(sysMaterialCoding,sysApiMaterialCoding);
        return sysApiMaterialCoding;
    }


}
